package com.rxt.thermopro.ui.fragment

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProfileBean
import kotlinx.android.synthetic.main.fragment_tp359_alert_setting.*

class TP359AlertSettingFragment : BaseFragment<BaseViewModel>() {

    override fun layoutId(): Int {
        return R.layout.fragment_tp359_alert_setting
    }

    companion object {
        private const val ARG_ADDRESS = "address"

        fun newInstance(): TP359AlertSettingFragment {
            return TP359AlertSettingFragment()
        }

        fun newInstance(address: String): TP359AlertSettingFragment {
            return TP359AlertSettingFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_ADDRESS, address)
                }
            }
        }
    }

    private var mAddresses = ""
    private lateinit var mTempProfileBean: ProfileBean
    private lateinit var mHumProfileBean: ProfileBean

    override fun initView(savedInstanceState: Bundle?) {
        mAddresses = arguments?.getString(ARG_ADDRESS, "") ?: ""
        tempText.text =
            String.format(getString(R.string.text_temp_alert, LocalDataDao.INSTANCE.getTempUnit()))
        humText.text =
            String.format(
                getString(
                    R.string.text_humidity_alert,
                    LocalDataDao.INSTANCE.getTempUnit()
                )
            )
        switchTemp.isSelected = LocalDataDao.INSTANCE.isTempAlarm359(mAddresses)
        switchHumidity.isSelected = LocalDataDao.INSTANCE.isHumAlarm359(mAddresses)
        tempMaxNum.text = LocalDataDao.INSTANCE.getTempProfile359(mAddresses).highTemperatureNum
        tempMinNum.text = LocalDataDao.INSTANCE.getTempProfile359(mAddresses).lowTemperatureNum

        humMaxNum.text = LocalDataDao.INSTANCE.getHumProfile359(mAddresses).highTemperatureNum
        humMinNum.text = LocalDataDao.INSTANCE.getHumProfile359(mAddresses).lowTemperatureNum

        mTempProfileBean = LocalDataDao.INSTANCE.getTempProfile359(mAddresses)
        mHumProfileBean = LocalDataDao.INSTANCE.getHumProfile359(mAddresses)
        setListener()
    }

    private fun setListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        viewAlertSetting.setOnClickListener {
            val intent = Intent()
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
            intent.data = Uri.fromParts("package", requireActivity().packageName, null)
            startActivity(intent)
        }

        switchTemp.setOnClickListener {
            switchTemp.isSelected = !switchTemp.isSelected
            LocalDataDao.INSTANCE.setTempAlarm359(switchTemp.isSelected, mAddresses)
        }

        switchHumidity.setOnClickListener {
            switchHumidity.isSelected = !switchHumidity.isSelected
            LocalDataDao.INSTANCE.setHumAlarm359(switchHumidity.isSelected, mAddresses)
        }

        buttonMaxAdd.setOnClickListener {
            updateSubAddValue(
                mTempProfileBean, tempMaxNum, isSub = false, isHighValue = true, maxValue = 60,
                minValue = -20, maxFValue = 140, minFValue = -4, hasFValue = true
            )
            LocalDataDao.INSTANCE.setTempProfile359(mAddresses,mTempProfileBean)
        }
        // 60  -20         99  10
        buttonMaxSub.setOnClickListener {
            updateSubAddValue(
                mTempProfileBean, tempMaxNum, isSub = true, isHighValue = true, maxValue = 60,
                minValue = -20, maxFValue = 140, minFValue = -4, hasFValue = true
            )
            LocalDataDao.INSTANCE.setTempProfile359(mAddresses,mTempProfileBean)
        }

        buttonMinAdd.setOnClickListener {
            updateSubAddValue(
                mTempProfileBean, tempMinNum, false, isHighValue = false, maxValue = 60,
                minValue = -20, maxFValue = 140, minFValue = -4, hasFValue = true
            )
            LocalDataDao.INSTANCE.setTempProfile359(mAddresses,mTempProfileBean)
        }

        buttonMinSub.setOnClickListener {
            updateSubAddValue(
                mTempProfileBean, tempMinNum, true, isHighValue = false, maxValue = 60,
                minValue = -20, maxFValue = 140, minFValue = -4, hasFValue = true
            )
            LocalDataDao.INSTANCE.setTempProfile359(mAddresses,mTempProfileBean)
        }

        buttonMaxAdd1.setOnClickListener {
            updateSubAddValue(
                mHumProfileBean, humMaxNum, false, isHighValue = true, maxValue = 99,
                minValue = 10, maxFValue = 99, minFValue = 10, hasFValue = false
            )
            LocalDataDao.INSTANCE.setHumProfile359(mAddresses,mHumProfileBean)
        }

        buttonMaxSub1.setOnClickListener {
            updateSubAddValue(
                mHumProfileBean, humMaxNum, true, isHighValue = true, maxValue = 99,
                minValue = 10, maxFValue = 99, minFValue = 10, hasFValue = false
            )
            LocalDataDao.INSTANCE.setHumProfile359(mAddresses,mHumProfileBean)
        }

        buttonMinAdd1.setOnClickListener {
            updateSubAddValue(
                mHumProfileBean, humMinNum, false, isHighValue = false, maxValue = 99,
                minValue = 10, maxFValue = 99, minFValue = 10, hasFValue = false
            )
            LocalDataDao.INSTANCE.setHumProfile359(mAddresses,mHumProfileBean)
        }

        buttonMinSub1.setOnClickListener {
            updateSubAddValue(
                mHumProfileBean, humMinNum, true, isHighValue = false, maxValue = 99,
                minValue = 10, maxFValue = 99, minFValue = 10, hasFValue = false
            )
            LocalDataDao.INSTANCE.setHumProfile359(mAddresses,mHumProfileBean)
        }
    }

    private fun updateSubAddValue(
        item: ProfileBean,
        textView: TextView,
        isSub: Boolean,
        isHighValue: Boolean,
        maxValue: Int,
        minValue: Int,
        maxFValue: Int,
        minFValue: Int,
        hasFValue: Boolean
    ) {
        if (isHighValue) {
            if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C || !hasFValue) {
                if (isSub) {
                    if (item.highTemperatureNum.toInt() <= item
                            .lowTemperatureNum.toInt() + 1
                    ) {
                        return
                    }
                    item.highTemperatureNum =
                        (item.highTemperatureNum.toInt() - 1).toString()
                } else {
                    if (item.highTemperatureNum.toInt() >= maxValue) {
                        return
                    }
                    item.highTemperatureNum =
                        (item.highTemperatureNum.toInt() + 1).toString()
                }
                textView.text = item.highTemperatureNum
                item.highValueF=LocalDataDao.INSTANCE.transUnitCToF(item.highTemperatureNum)
            } else {
                if (isSub) {
                    if (item.highValueF.toInt() <= item
                            .lowValueF.toInt() + 1
                    ) {
                        return
                    }
                    item.highValueF =
                        (item.highValueF.toInt() - 1).toString()
                } else {
                    if (item.highValueF.toInt() >= maxFValue) {
                        return
                    }
                    item.highValueF =
                        (item.highValueF.toInt() + 1).toString()
                }
                textView.text =
                    item.highValueF
                item.highTemperatureNum=LocalDataDao.INSTANCE.transUnitFToC(item.highValueF)
            }
        } else {
            if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C || !hasFValue) {
                if (isSub) {
                    if (item.lowTemperatureNum.toInt() <= minValue) {
                        return
                    }
                    item.lowTemperatureNum =
                        (item.lowTemperatureNum.toInt() - 1).toString()
                } else {
                    if (item.lowTemperatureNum.toInt() >= item.highTemperatureNum.toInt() - 1) {
                        return
                    }
                    item.lowTemperatureNum =
                        (item.lowTemperatureNum.toInt() + 1).toString()
                }
                textView.text = item.lowTemperatureNum
                item.lowValueF=LocalDataDao.INSTANCE.transUnitCToF(item.lowTemperatureNum)
            } else {
                if (isSub) {
                    if (item.lowValueF.toInt() <= minFValue) {
                        return
                    }
                    item.lowValueF =
                        (item.lowValueF.toInt() - 1).toString()
                } else {
                    if (item.lowValueF.toInt() >= item.highValueF.toInt() + 1) {
                        return
                    }
                    item.lowValueF =
                        (item.lowValueF.toInt() + 1).toString()
                }
                textView.text =
                    item.lowValueF
                item.lowTemperatureNum=LocalDataDao.INSTANCE.transUnitFToC(item.lowValueF)
            }
        }

    }

}